<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

class kematian extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('tanggal');
    }

    function form_kematian() {
        
        $this->template->load('halaman_v', 'kematian/tambah_kematian_v');
    }

    function tambah(){
        if($this->input->post('tambah') !=NULL)
        {
            $data['idkematian'] = $this->input->post('idkematian');
            $data['no_form'] = $this->input->post('no_form');
            $data['no_kk'] = $this->input->post('no_kk');
            $data['nik'] = $this->input->post('nik_mati');
            $data['tgl_mati'] = $this->input->post('tgl_mati');
            $data['pukul'] = $this->input->post('pukul');
            $data['tempat_kematian'] = $this->input->post('tempat_kematian');
            $data['desa_kematian'] = $this->input->post('desa_kematian');
            $data['kab_kematian'] = $this->input->post('kab_kematian');
            $data['sebab'] = $this->input->post('sebab');
            $data['penerang'] = $this->input->post('penerang');
            $data['nik_pelapor'] = $this->input->post('nik_pelapor');
            $data['nama_pelapor'] = $this->input->post('nama_pelapor');
            $data['hub_pelapor'] = $this->input->post('hub_pelapor');
            $data['nik_saksi_satu'] = $this->input->post('nik_saksi_satu');
            $data['nama_saksi_satu'] = $this->input->post('nama_saksi_satu');
            $data['nik_saksi_dua'] = $this->input->post('nik_saksi_dua');
            $data['nama_saksi_dua'] = $this->input->post('nama_saksi_dua');
            
            $this->load->model('umum_m');
            if ($this->umum_m->cek_relasi_tabel('penduduk','nik',$data['nik'])>0){
                $this->umum_m->ubah('penduduk',array('status'=>0),'nik',$data['nik']); //yg dirubah
                $this->umum_m->tambah('kematian',$data);
            }
            $this->tampil();
        }else{
            echo 'GAGAL!';
        }
    }
    
    function ubah($id = NULL) {
        if ($id != NULL) {
            $this->load->model('umum_m');
            $data['data_kematian'] = $this->umum_m->ambil_data('kematian', 'idkematian = ' . $id)->row();
            $this->template->load('halaman_v', 'kematian/ubah_kematian_v', $data);
        } else {
            redirect('kematian');
        }
    }
    
    function simpan_ubah() { //kalau data tidak diubah,maka....?
        if ($this->input->post() != null) {
            $kematian['idkematian'] = $this->input->post('idkematian');
            $kematian['no_form'] = $this->input->post('no_form');
            $kematian['no_kk'] = $this->input->post('no_kk');
            $kematian['nik'] = $this->input->post('nik_mati');
            $kematian['tgl_mati'] = $this->input->post('tgl_mati');
            $kematian['pukul'] = $this->input->post('pukul');
            $kematian['tempat_kematian'] = $this->input->post('tempat_kematian');
            $kematian['desa_kematian'] = $this->input->post('desa_kematian');
            $kematian['kab_kematian'] = $this->input->post('kab_kematian');
            $kematian['sebab'] = $this->input->post('sebab');
            $kematian['penerang'] = $this->input->post('penerang');
            $kematian['nik_pelapor'] = $this->input->post('nik_pelapor');
            $kematian['nama_pelapor'] = $this->input->post('nama_pelapor');
            $kematian['hub_pelapor'] = $this->input->post('hub_pelapor');
            $kematian['nik_saksi_satu'] = $this->input->post('nik_saksi_satu');
            $kematian['nama_saksi_satu'] = $this->input->post('nama_saksi_satu');
            $kematian['nik_saksi_dua'] = $this->input->post('nik_saksi_dua');
            $kematian['nama_saksi_dua'] = $this->input->post('nama_saksi_dua');

            $this->load->model('umum_m');
            if ($this->umum_m->ubah('kematian', $kematian, 'idkematian', $kematian['idkematian'])) {
                redirect('kematian/tampil');
            }
        }
    }

    function tampil() {
        $this->load->model('kematian_m');
        $data['kematian'] = $this->kematian_m->ambil_data();
        $this->template->load('halaman_v', 'kematian/tampil_kematian_v',$data);
    }
    
    function hapus($id = NULL){
        $this->load->model('umum_m');
        $this->umum_m->hapus_data_tabel('kematian', array('idkematian'=>$id));
        $this->tampil();
    }
    
    function cari_jk() {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        if ($this->input->post() != NULL) {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari(array('penduduk.idjenis_kelamin' => $this->input->post('idjenis_kelamin')));
            $data['nama_jeniskelamin'] = $this->umum_m->native_query('select jenis_kelamin from jenis_kelamin where idjenis_kelamin=' . $this->input->post('idjenis_kelamin'))->row();
            $this->template->load('halaman_v', 'kematian/cari_jkkematian_v', $data);
        } else {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari();
            $this->template->load('halaman_v', 'kematian/cari_jkkematian_v', $data);
        }
    }
    
    function cari_nik() {
        $this->load->model('umum_m');
        $data['kematian'] = $this->umum_m->ambil_data('kematian');
        if ($this->input->post() != NULL) {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari(array('penduduk.nik' => $this->input->post('nik')));
            $data['nik'] = $this->umum_m->native_query('select nik from penduduk where nik=' . $this->input->post('nik'))->row();
            $this->template->load('halaman_v', 'kematian/cari_formkematian_v', $data);
        } else {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari();
            $this->template->load('halaman_v', 'kematian/cari_formkematian_v', $data);
        }
    }
    
    function cetak_suratketerangan() {
        $this->load->model('umum_m');
        $data['kematian'] = $this->umum_m->ambil_data('kematian');
        if ($this->input->post() != NULL) {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari(array('penduduk.nik' => $this->input->post('nik')));
            $data['nik'] = $this->umum_m->native_query('select nik from penduduk where nik=' . $this->input->post('nik'))->row();
            $this->template->load('halaman_v', 'kematian/cetak_formkematian_v', $data);
        } else {
            $this->load->model('kematian_m');
            $data['kematian'] = $this->kematian_m->detail_cari();
            $this->template->load('halaman_v', 'kematian/cetak_formkematian_v', $data);
        }
    }
}

?>
